/// https://github.com/dart-lang/site-www/issues/468
/// https://github.com/dart-lang/sdk/issues/34284

import 'dart:async';
import 'dart:io';

void main() async {
  var secure = true;
  var HOST_NAME = '127.0.0.1';
  // var HOST_NAME = '192.168.50.172';
  // var HOST_NAME = '192.168.50.53';
  // var HOST_NAME = '122.51.162.249';
  var port = 44444;
  var url = '${secure ? "wss" : "ws"}://$HOST_NAME:$port';
  // var url = 'wss://echo.websocket.org';
  var data = 'hello, world.';
  var complete = Completer();
  try {
    print('开始新连接... ${url}');
    var socket = await WebSocket.connect(url);
    socket.listen((data) {
      print('$data');
      socket.close();
    }, onDone: () {
      print('done');
      complete.complete();
    }, onError: (error) {
      print('$error');
      complete.complete();
    });
    socket.add(data);
  } catch (e) {
    print('$e');
    complete.complete();
  }
  return complete.future;
}
